package com.shimao.celnet.payment.controller.apinotcheck;

import com.alibaba.fastjson.JSON;
import com.baiwang.bop.client.IBopClient;
import com.baiwang.bop.request.impl.invoice.impl.InvoiceOpenRequest;
import com.baiwang.bop.respose.entity.InvoiceOpenResponse;
import com.baiwang.bop.respose.entity.InvoiceQueryResponse;
import com.shimao.celnet.payment.config.ConstantConfig;
import com.shimao.celnet.payment.form.IAppInterfaceForm;
import com.shimao.celnet.payment.form.PInvoiceForm;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.service.IAppInterfaceService;
import com.shimao.celnet.payment.service.PInvoiceBlueService;
import com.shimao.celnet.payment.service.PInvoiceRedService;
import com.shimao.celnet.payment.service.apiservice.CallOut_invOpenOutService;
import com.shimao.celnet.payment.service.apiservice.CallIn_outInvOpenLimitBuleReqInService;
import com.shimao.celnet.payment.util.*;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Christ
 * @Description: 发票开具接口；支付平台--》税控系统
 * 2018/1/18
 */
@RestController
@Service
@RequestMapping(value = "/invoice")
public class CallOut_invOpenOutController {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private CallOut_invOpenOutService invOpenOutService;

    @Autowired
    private PInvoiceBlueService pInvoiceBlueService;

    @Autowired
    private PInvoiceRedService pInvoiceRedService;

    @Autowired
    private CallOut_invoiceQueryOutController callOut_invoiceQueryOutController;

    @Autowired
    private CallOut_invOpenRetInfoOutController callOut_invOpenRetInfoOutController;

    @Autowired
    private CallOut_invPdfOutController callOut_invPdfOutController;

    @Autowired
    private IAppInterfaceService iAppInterfaceService;

    @Autowired
    private HttpSendPostUtil httpSendPostUtil;

    @Autowired
    private ConstantConfig constantConfig;

    @Autowired
    private GetJFSystemToken getJFSystemToken;

    @Autowired//拆票service
    private CallIn_outInvOpenLimitBuleReqInService callIn_outInvOpenLimitBuleReqInService;

    /**
     * 开票的业务情况
     * 1，开蓝票
     * 【1】，通过pc界面开具蓝票
     * (1)开个人蓝票
     * （2）开企业蓝票
     * 【2】，立即开具蓝票
     * 【3】，app开蓝票
     * 2，开红票
     * 【1】，开红票不经过界面红冲
     */

    //【1】，通过pc界面开具蓝票 （2）企业蓝票
    @RequestMapping(value = "/openByCompany", method = {RequestMethod.POST})
    public void invoiceBlueFromPCompanyRequest(@RequestBody PInvoiceForm pinvoice) throws Exception {
        //获取请求PC开票页面中传入的开票记录的guid
        String invoiceGuid = pinvoice.getGuid();
        JSONObject RetJsonObject = new JSONObject();
        //根据guid查询出蓝票信息
        PInvoiceBlue pInvoiceBlue = invOpenOutService.getPInvoiceBlue(invoiceGuid);
        //在开票公共界面填写的抬头信息,传递到后台类，用来拼凑请求百望的参数
        Map map = new HashMap();
        map.put("pInvoiceBlue", pInvoiceBlue);
        map.put("BuyerTaxNo", pinvoice.getBuyerTaxNo());//购方单位税号
        map.put("BuyerName", pinvoice.getBuyerName());//购方单位名称
        map.put("BuyerAddressPhone", pinvoice.getBuyerAddressPhone());//购方地址及电话专票必填
        map.put("BuyerBankAccount", pinvoice.getBuyerBankAccount());//购方开户行及账号专票必填
        map.put("invoiceTypeCode", pinvoice.getInvoiceTypeCode());//发票类型
        //保存开票信息
        pInvoiceBlue.setBuyerAddressPhone(pinvoice.getBuyerAddressPhone());
        pInvoiceBlue.setBuyerTaxNo(pinvoice.getBuyerTaxNo());
        pInvoiceBlue.setBuyerName(pinvoice.getBuyerName());
        pInvoiceBlue.setBuyerBankAccount(pinvoice.getBuyerBankAccount());
        pInvoiceBlue.setBuyerEmail(pinvoice.getBuyerEmail());
		pInvoiceBlue.setBuyerPhone(pinvoice.getBuyerPhone());        //购方电话，即时开票标识为1，且电子发票时，电话与邮箱其一必须
        pInvoiceBlue.setInvoiceTypeCode(pinvoice.getInvoiceTypeCode());
        pInvoiceBlueService.update(pInvoiceBlue); //更新发票记录
        //判断是否需要拆票
       /* if (Long.valueOf(pInvoiceBlue.getInvoiceTotalPriceTax()) >= Long.valueOf(pInvoiceBlue.getLimitAccount()==null?0L:Long.valueOf(pInvoiceBlue.getLimitAccount())) && pInvoiceBlue.getLimitAccount() != null && pInvoiceBlue.getLimitAccount() != "" && !pInvoiceBlue.getLimitAccount().contains(".")){
            callIn_outInvOpenLimitBuleReqInService.outInvOpenLimitBuleReqInService(pInvoiceBlue);
            return;
        }*/
        //调用公共拼接参数的方法makeRequestClass
        InvoiceOpenRequest request = invOpenOutService.makeRequestClass(map);
        //调用开具发票
        InvoiceOpenResponse response = new InvoiceOpenResponse();
        try {
            response = publicOpenInvoice(request);
        } catch (Exception e) {
            throw new MyException(e.getMessage());
        }
        //开票成功后保存蓝票数据
        if (response != null) {
            //开票成功后的信息加入到开票记录中
            pInvoiceBlue.setInvoiceCode(response.getInvoiceCode());//发票代码
            pInvoiceBlue.setInvoiceNo(response.getInvoiceNo());//发票号码
            pInvoiceBlue.setInvoiceEndTime(String.valueOf(MyDateUtil.StrToDate(response.getInvoiceDate(), MyDateUtil.format_yyyyMMddHHmmss).getTime()));//开票完成日期
            pInvoiceBlue.setTaxControlCode(response.getTaxControlCode());//税控码 对应发票的密码区
            pInvoiceBlue.setInvoiceCheckCode(response.getInvoiceCheckCode());//校验码
            pInvoiceBlue.setInvoiceQrcode(response.getInvoiceQrCode());//二维码
            pInvoiceBlue.setInvoiceStatus(FinalUtil.INVOICE_SUCCESS);//更新发票状态;开票成功
            pInvoiceBlueService.update(pInvoiceBlue); //更新发票记录
        }
//        if (pInvoiceBlue != null) {
            String strUrl = null;
            //  3、调用百望开票结果查询接口，查询开票结果
            InvoiceQueryResponse responseRet = callOut_invoiceQueryOutController.invoiceBlueQueryOut(pInvoiceBlue);
            //返回结果
            if (FinalUtil.INVOICE_TYPE_CODE_026.equals(pInvoiceBlue.getInvoiceTypeCode())) {
                strUrl = callOut_invPdfOutController.invPdfOut(pInvoiceBlue);
            }
            //如果查询结果查询失败，则直接查询本地的数据去回调。
            if(responseRet == null){
                //返回结果
                 RetJsonObject = callOut_invOpenRetInfoOutController.invOpenRetBlueInfoOut(response, pInvoiceBlue, strUrl);
            }else{
                //返回结果
                 RetJsonObject = callOut_invOpenRetInfoOutController.invOpenRetBlueInfoOut(responseRet, pInvoiceBlue, strUrl);
            }

            List<IAppInterfaceForm> iAppInterfaceFormsList = getRetUrl(pInvoiceBlue.getAppKey(), pInvoiceBlue.getInterfaceId());

            for (IAppInterfaceForm iAppInterfaceForm : iAppInterfaceFormsList) {
                 //如果是打印回调接口直接continue
                if ("20180117".equals(pInvoiceBlue.getAppKey()) && "1008".equals(iAppInterfaceForm.getInterfaceId())) {
                continue; //直接执行下一条
                 }
                //当要通知物业计费系统的时候，需要制定回调的接口应用
                if ("20180117".equals(pInvoiceBlue.getAppKey()) && "1005".equals(iAppInterfaceForm.getInterfaceId())) {
                    /**
                     * 物业计费的接口验证规则
                     * 只接受来自物业的费用
                     */
                    String token = getJFSystemToken.getJFSystemNewToken(pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                    String ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    logger.info(RetJsonObject);
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    logger.info(ret);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                    String code = retObj.getString("errcode");
                    logger.info("*********" + code);

                    if("6001".equals(code)){  //发生无效token时  ；重新获取
                        token = getJFSystemToken.getJFSystemNewToken6001(pInvoiceBlue.getAppKey(),iAppInterfaceForm.getInterfaceId());
                        //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                        ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token,pInvoiceBlue.getAppKey(),iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        logger.info(RetJsonObject);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                    }

                    if ("0".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);

                    } else {
                        token = getJFSystemToken.getJFSystemNewToken(pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                        ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(RetJsonObject);
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                        logger.info("*********" + code);
                        if ("0".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                     return; //一个系统值通知一遍
                }else if ("20180913".equals(pInvoiceBlue.getAppKey()) && "1009".equals(iAppInterfaceForm.getInterfaceId())){
                    //O家APP推送，
                    logger.info("@@@@@@@@@@@O家APP推送参数：" +RetJsonObject.toString());
                    String ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    logger.info(ret);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                    String code = retObj.getString("errcode");
                    logger.info("@@@@@@@@@@@" + code);
                    if ("0000".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);
                    } else {
                        logger.info("@@@@@@@@@@@O家APP推送参数：" +RetJsonObject.toString());
                        ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                        logger.info("@@@@@@@@@@@" + code);
                        if ("0000".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                    return; //一个系统值通知一遍
                } else  if ("20171221".equals(pInvoiceBlue.getAppKey()) && "1006".equals(iAppInterfaceForm.getInterfaceId())){
                    //O家APP推送，
                    logger.info("@@@@@@@@@@@停车系统" +RetJsonObject.toString());
                    String ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    logger.info(ret);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                    String code = retObj.getString("errcode");
                    logger.info("@@@@@@@@@@@" + code);
                    if ("0000".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);
                    } else {
                        logger.info("@@@@@@@@@@@停车系统：" +RetJsonObject.toString());
                        ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                        logger.info("@@@@@@@@@@@" + code);
                        if ("0000".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                    return; //一个系统值通知一遍
                } else{
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    String result = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject,pInvoiceBlue.getAppKey(),iAppInterfaceForm.getInterfaceId());
                    logger.info(result);
                    logger.info(RetJsonObject);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(result);
                    String code = retObj.getString("errcode");
                    logger.info("@@@@@@@@@@@" + code);
                    if ("0000".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);
                    } else {
                        logger.info("@@@@@@@@@@@：" +RetJsonObject.toString());
                        result = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(result);
                        retObj = JSON.parseObject(result);
                        code = retObj.getString("errcode");
                        logger.info("@@@@@@@@@@@" + code);
                        if ("0000".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                }
            }
//        }
    }

    //【1】，通过pc界面开具蓝票 （2）个人蓝票
    @RequestMapping(value = "/openByPerson", method = {RequestMethod.POST})
    public void invoiceBlueFromPCRequest(@RequestBody PInvoiceForm pinvoice) throws Exception {
        //获取请求PC开票页面中传入的开票记录的guid
        String invoiceGuid = pinvoice.getGuid();
        //回调的json串
        JSONObject RetJsonObject = new JSONObject();
        //根据guid查询出蓝票信息
        PInvoiceBlue pInvoiceBlue = invOpenOutService.getPInvoiceBlue(invoiceGuid);
        //在开票公共界面填写的抬头信息,传递到后台类，用来拼凑请求百望的参数
        Map map = new HashMap();
        map.put("pInvoiceBlue", pInvoiceBlue);
        map.put("BuyerName", pinvoice.getBuyerName());//购方单位名称
        map.put("invoiceTypeCode", pinvoice.getInvoiceTypeCode());//发票类型
        //保存开票信息
		pInvoiceBlue.setBuyerPhone(pinvoice.getBuyerPhone());        //购方电话，即时开票标识为1，且电子发票时，电话与邮箱其一必须
        pInvoiceBlue.setBuyerEmail(pinvoice.getBuyerEmail());//购方邮箱
        pInvoiceBlue.setBuyerName(pinvoice.getBuyerName());//购方单位名称
        pInvoiceBlue.setInvoiceTypeCode(pinvoice.getInvoiceTypeCode());//发票类型
        pInvoiceBlueService.update(pInvoiceBlue); //更新发票记录
        //判断是否需要拆票
        /*if (Long.valueOf(pInvoiceBlue.getInvoiceTotalPriceTax()) >= Long.valueOf(pInvoiceBlue.getLimitAccount()==null?0L:Long.valueOf(pInvoiceBlue.getLimitAccount())) && pInvoiceBlue.getLimitAccount() != null && pInvoiceBlue.getLimitAccount() != ""){
            callIn_outInvOpenLimitBuleReqInService.outInvOpenLimitBuleReqInService(pInvoiceBlue);
            return;
        }*/
        //调用公共拼接参数的方法makeRequestClass
        InvoiceOpenRequest request = invOpenOutService.makeRequestClass(map);
        //调用开具发票
        InvoiceOpenResponse response = new InvoiceOpenResponse();
        try {
            response = publicOpenInvoice(request);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new MyException(e.getMessage());
        }
        //开票成功后保存蓝票数据
        if (response != null) {
            //开票成功后的信息加入到开票记录中
            pInvoiceBlue.setInvoiceCode(response.getInvoiceCode());//发票代码
            pInvoiceBlue.setInvoiceNo(response.getInvoiceNo());//发票号码
            pInvoiceBlue.setInvoiceEndTime(String.valueOf(MyDateUtil.StrToDate(response.getInvoiceDate(), MyDateUtil.format_yyyyMMddHHmmss).getTime()));//开票完成日期
            pInvoiceBlue.setTaxControlCode(response.getTaxControlCode());//税控码 对应发票的密码区
            pInvoiceBlue.setInvoiceCheckCode(response.getInvoiceCheckCode());//校验码
            pInvoiceBlue.setInvoiceQrcode(response.getInvoiceQrCode());//二维码
            pInvoiceBlue.setInvoiceStatus(FinalUtil.INVOICE_SUCCESS);//更新发票状态;开票成功
            pInvoiceBlueService.update(pInvoiceBlue); //更新发票记录
        }

            String strUrl = null;
            // 3、调用百望开票结果查询接口，查询开票结果
            InvoiceQueryResponse responseRet = callOut_invoiceQueryOutController.invoiceBlueQueryOut(pInvoiceBlue);
            if (FinalUtil.INVOICE_TYPE_CODE_026.equals(pInvoiceBlue.getInvoiceTypeCode())) {
                strUrl = callOut_invPdfOutController.invPdfOut(pInvoiceBlue);
            }
            //如果查询结果查询失败，则直接查询本地的数据去回调。
            if(responseRet == null){
                //返回结果
                RetJsonObject = callOut_invOpenRetInfoOutController.invOpenRetBlueInfoOut(response, pInvoiceBlue, strUrl);
            }else{
                //返回结果
                RetJsonObject = callOut_invOpenRetInfoOutController.invOpenRetBlueInfoOut(responseRet, pInvoiceBlue, strUrl);
            }
            List<IAppInterfaceForm> iAppInterfaceFormsList = getRetUrl(pInvoiceBlue.getAppKey(), pInvoiceBlue.getInterfaceId());

            for (IAppInterfaceForm iAppInterfaceForm : iAppInterfaceFormsList) {
                //如果是打印回调接口直接continue
                if ("20180117".equals(pInvoiceBlue.getAppKey()) && "1008".equals(iAppInterfaceForm.getInterfaceId())) {
                    continue; //直接执行下一条
                }
                //当要通知物业计费系统的时候，需要制定回调的接口应用
                if ("20180117".equals(pInvoiceBlue.getAppKey()) && "1005".equals(iAppInterfaceForm.getInterfaceId())) {
                    /**
                     * 物业计费的接口验证规则
                     * 只接受来自物业的费用
                     */
                    String token = getJFSystemToken.getJFSystemNewToken(pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                    String ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    logger.info(RetJsonObject);
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    logger.info(ret);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                    String code = retObj.getString("errcode");
                    logger.info("*********" + code);

                    if("6001".equals(code)){  //发生无效token时  ；重新获取
                        token = getJFSystemToken.getJFSystemNewToken6001(pInvoiceBlue.getAppKey(),iAppInterfaceForm.getInterfaceId());
                        //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                        ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token,pInvoiceBlue.getAppKey(),iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        logger.info(RetJsonObject);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                    }

                    if ("0".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);

                    } else {
                        token = getJFSystemToken.getJFSystemNewToken(pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                        ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(RetJsonObject);
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                        logger.info("*********" + code);
                        if ("0".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                    return; //一个系统值通知一遍
                }else if ("20180913".equals(pInvoiceBlue.getAppKey()) && "1009".equals(iAppInterfaceForm.getInterfaceId())){
                    //O家APP推送，
                    logger.info("@@@@@@@@@@@O家APP推送参数：" +RetJsonObject.toString());
                    String ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    logger.info(ret);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                    String code = retObj.getString("errcode");
                    logger.info("@@@@@@@@@@@O家APP推送" + code);
                    if ("0000".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);
                    } else {
                        //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                        ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(RetJsonObject);
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                        logger.info("@@@@@@@@@@@O家APP推送" + code);
                        if ("0000".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                    return; //一个系统值通知一遍
                }else if ("20171221".equals(pInvoiceBlue.getAppKey()) && "1006".equals(iAppInterfaceForm.getInterfaceId())){
                    //O家APP推送，
                    logger.info("@@@@@@@@@@@停车系统" +RetJsonObject.toString());
                    String ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    logger.info(ret);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                    String code = retObj.getString("errcode");
                    logger.info("@@@@@@@@@@@" + code);
                    if ("0000".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);
                    } else {
                        logger.info("@@@@@@@@@@@停车系统：" +RetJsonObject.toString());
                        ret = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(ret);
                        retObj = JSON.parseObject(ret);
                        code = retObj.getString("errcode");
                        logger.info("@@@@@@@@@@@" + code);
                        if ("0000".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                    return; //一个系统值通知一遍
                } else{
                    logger.info(iAppInterfaceForm.getRequestUrl());
                    String result = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject,pInvoiceBlue.getAppKey(),iAppInterfaceForm.getInterfaceId());
                    logger.info(result);
                    logger.info(RetJsonObject);
                    com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(result);
                    String code = retObj.getString("errcode");
                    logger.info("@@@@@@@@@@@" + code);
                    if ("0000".equals(code)) {
                        //保存数据库，更改状态
                        pInvoiceBlue.setIsPush("1");
                        pInvoiceBlueService.update(pInvoiceBlue);
                    } else {
                        logger.info("@@@@@@@@@@@：" +RetJsonObject.toString());
                        result = httpSendPostUtil.sendPost1(iAppInterfaceForm.getRequestUrl(), RetJsonObject, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                        logger.info(iAppInterfaceForm.getRequestUrl());
                        logger.info(result);
                        retObj = JSON.parseObject(result);
                        code = retObj.getString("errcode");
                        logger.info("@@@@@@@@@@@" + code);
                        if ("0000".equals(code)) {
                            //保存数据库，更改状态
                            pInvoiceBlue.setIsPush("1");
                            pInvoiceBlueService.update(pInvoiceBlue);
                        }

                    }
                }
            }

//        }
    }

    //【3】，app开蓝票
    @RequestMapping(value = "/openByApp", method = {RequestMethod.POST})
    public void invoiceBlueFromAppRequest(@RequestBody PInvoiceForm pinvoice) throws Exception {
        //获取请求APP开票页面中传入的开票记录的类型：1:企业 0:个人
        String invoiceHeadType = pinvoice.getHeadType();
        //如果是个人抬头
        if (FinalUtil.INVOICE_HEAD_TYPE_PERSON.equals(invoiceHeadType)) {
            this.invoiceBlueFromPCRequest(pinvoice);
        } else {
            this.invoiceBlueFromPCompanyRequest(pinvoice);
        }
    }

    //【2】，立即开具蓝票
    public PInvoiceBlue invoiceBlueFromImmRequest(PInvoiceBlue pInvoiceBlue) {
        //在开票公共界面填写的抬头信息,传递到后台类，用来拼凑请求百望的参数
        Map map = new HashMap();
        map.put("pInvoiceBlue", pInvoiceBlue);
        map.put("BuyerTaxNo", pInvoiceBlue.getBuyerTaxNo());//购方单位税号
        map.put("BuyerName", pInvoiceBlue.getBuyerName());//购方单位名称
        map.put("BuyerAddressPhone", pInvoiceBlue.getBuyerAddressPhone());//购方地址及电话专票必填
        map.put("BuyerBankAccount", pInvoiceBlue.getBuyerBankAccount());//购方开户行及账号专票必填
        //调用公共拼接参数的方法makeRequestClass
        InvoiceOpenRequest request = invOpenOutService.makeRequestClass(map);
        //调用开具发票
        InvoiceOpenResponse response = publicOpenInvoice(request);
        //开票成功后保存蓝票数据
        if (response != null) {
            //开票成功后的信息加入到开票记录中
            pInvoiceBlue.setInvoiceCode(response.getInvoiceCode());//发票代码
            pInvoiceBlue.setInvoiceNo(response.getInvoiceNo());//发票号码
            pInvoiceBlue.setInvoiceEndTime(String.valueOf(MyDateUtil.StrToDate(response.getInvoiceDate(), MyDateUtil.format_yyyyMMddHHmmss).getTime()));//开票完成日期
            pInvoiceBlue.setTaxControlCode(response.getTaxControlCode());//税控码 对应发票的密码区
            pInvoiceBlue.setInvoiceCheckCode(response.getInvoiceCheckCode());//校验码
            pInvoiceBlue.setInvoiceQrcode(response.getInvoiceQrCode());//二维码
            pInvoiceBlue.setInvoiceStatus(FinalUtil.INVOICE_SUCCESS);//更新发票状态;开票成功
            System.out.println(pInvoiceBlue.getGuid());
            pInvoiceBlueService.update(pInvoiceBlue); //更新发票记录
        }
        return pInvoiceBlue;
    }

    //【1】，开红票不经过界面红冲
    public PInvoiceRed invoiceRedFromRequest(PInvoiceRed pInvoiceRed) {
        //在开票公共界面填写的抬头信息,传递到后台类，用来拼凑请求百望的参数
        Map map = new HashMap();
        map.put("pInvoiceRed", pInvoiceRed);
        map.put("BuyerTaxNo", pInvoiceRed.getBuyerTaxNo());//购方单位税号
        map.put("BuyerName", pInvoiceRed.getBuyerName());//购方单位名称
        map.put("BuyerAddressPhone", pInvoiceRed.getBuyerAddressPhone());//购方地址及电话专票必填
        map.put("BuyerBankAccount", pInvoiceRed.getBuyerBankAccount());//购方开户行及账号专票必填
        //调用公共拼接参数的方法makeRequestClass
        InvoiceOpenRequest request = invOpenOutService.makeRequestClass(map);
        //调用开具发票
        InvoiceOpenResponse response = publicOpenInvoice(request);
        //开票成功后保存蓝票数据
        if (response != null) {
            //开票成功后的信息加入到开票记录中
            pInvoiceRed.setInvoiceCode(response.getInvoiceCode());//发票代码
            pInvoiceRed.setInvoiceEndTime(String.valueOf(MyDateUtil.StrToDate(response.getInvoiceDate(), MyDateUtil.format_yyyyMMddHHmmss).getTime()));//开票完成日期
            pInvoiceRed.setInvoiceNo(response.getInvoiceNo());//发票号码
            pInvoiceRed.setTaxControlCode(response.getTaxControlCode());//税控码 对应发票的密码区
            pInvoiceRed.setInvoiceCheckCode(response.getInvoiceCheckCode());//校验码
            pInvoiceRed.setInvoiceQrcode(response.getInvoiceQrCode());//二维码
            pInvoiceRed.setInvoiceStatus(FinalUtil.INVOICE_SUCCESS);//更新发票状态;开票成功
            pInvoiceRedService.update(pInvoiceRed); //更新发票记录
        }
        return pInvoiceRed;
    }

    public InvoiceOpenResponse publicOpenInvoice(InvoiceOpenRequest request) {
        String token = invOpenOutService.getToken();
        IBopClient client = invOpenOutService.getClient();
        InvoiceOpenResponse response = client.execute(request, token, InvoiceOpenResponse.class);
        return response;
    }

    public List<IAppInterfaceForm> getRetUrl(String pAppKey, String pInterfaceId) {
        //============1,根据app_key和interface_id查询出i_app_interface表的guid====
        IAppInterfaceForm iAppInterfaceForm = iAppInterfaceService.getInfoByAppIfKey(pAppKey, pInterfaceId);
        //============3，根据guid，查询i_app_interface_asyn表所有需要回调的系统的returnUrl====
        logger.info("guid" + iAppInterfaceForm.getGuid());
        List<IAppInterfaceForm> iAppInterfaceFormList = iAppInterfaceService.getRetAppInterface(iAppInterfaceForm.getGuid());
        return iAppInterfaceFormList;
    }




}